/*Welfare Exercise*/

clear

/*Assumptions*/
local gammahat = 0.157
local icempg = 30
local BEVmpkwh = 4 
local basevmtperyear = 10793 /*Possible alternatives: EPA assumption of 15000 per year, NHTSA mean "bestmile" for CA for 2017 = 10793, NHTSA mean "bestmile" for CA, 2017, hybrid vehicles = 12522.6*/ 
local numyears = 1
local discountfactor = 0.99999
local basevmt = `basevmtperyear'*`numyears'
*local basevmt = (1/(1-`discountfactor'))*`basevmtperyear'*(1-`discountfactor'^`numyears') /*10 years discounted*/

/*Cents per mile prior for EV*/ /*EPA sticker for Tesla 3, $500 fuel costs for 15k miles @ $0.13 /kwh*.  Per-mile estimate = 500 / 15000 = 0.033.  Note: EPA sticker notes you save $4,500 over five years  relative to 26mpg average vehicle*/
/*https://www.teslarati.com/wp-content/uploads/2017/09/tesla-model-3-long-range-window-sticker.jpg*/

local theta = 3.3
local theta_p_perkwh = `theta'*`BEVmpkwh'


/*Inputs from other sources*/

/*Externalities - phi_g / phi_e*/
/*HMMY 2016, Table 4*/  
/*ICE externality per mile in CA*/ local phi_g = 2.55
/*EV externality per mile in CA*/ local phi_e = 0.69

/*Electricity Prices*/
/*Source: BB, Fig 3*/
local marg_elec_price = 30.1

/*Source: BB, Fig 5*/
/*Source: Jim B email, rough average from 2014- 2016*/
local PMC_elec = 3.6
 
/*Electricity Tax per mile in CA*/ 
local tau_e = (`marg_elec_price' - `PMC_elec') / `BEVmpkwh'

/*Electricity cost per mile*/
local p_e = `marg_elec_price' / `BEVmpkwh'

/*Gas Taxes in CA*/

/*Source: EIA,   https://docs.google.com/viewer?url=https%3A%2F%2Fwww.eia.gov%2Fpetroleum%2Fmarketing%2Fmonthly%2Fxls%2Ffueltaxes.xls*/
local gastax = 68.3

local tau_g = `gastax'/`icempg'

macro list

/*Graph of optimal subsidy as a function of gamma*/

clear
set obs 1000
gen gamma = _n/1000

gen fb_subsidy_externality = `basevmt'*((`phi_g'-`tau_g')-(`phi_e'-`tau_e')) / 100

gen fb_subsidy_internality = -(1-gamma)*`basevmt'*((`p_e'-`theta')) / 100

gen fb_subsidy_total = fb_subsidy_externality+fb_subsidy_internality


label var fb_subsidy_externality "Unpriced Externality (A)"
label var fb_subsidy_internality "Unpriced Internality (B)"
label var fb_subsidy_total "Total Subsidy (= A + B)"

set scheme s1color

graph twoway (line fb_subsidy_externality fb_subsidy_internality gamma, lp(dash dash)) (area fb_subsidy_total gamma, color(gs10%50)), ///
yscale(range(-500 1000)) ylabel(-500(500)1000) ///
xtitle("Gamma") ytitle("Optimal annual subsidy (dollars)")  ///
yline(0, lc(black)) ///
xline(`gammahat', lp(solid) lc(red%50)) 


graph export "${ResultsOut}/Figures/subsidy_and_gamma_20240508.png", replace

/*Graph of optimal subsidy as a function of gas tax*/

clear
set obs 1000
gen gast = 150*_n/1000 

gen tau_g = gast/`icempg'

gen fb_subsidy_externality = `basevmt'*((`phi_g'-tau_g)-(`phi_e'-`tau_e')) / 100

gen fb_subsidy_internality = -(1-`gammahat')*`basevmt'*((`p_e'-`theta')) / 100

gen fb_subsidy_total = fb_subsidy_externality+fb_subsidy_internality


label var fb_subsidy_externality "Unpriced Externality"
label var fb_subsidy_internality "Unpriced Internality"
label var fb_subsidy_total "Total Subsidy (= Externality + Internality)"

set scheme s1color

graph twoway (line fb_subsidy_externality fb_subsidy_internality gast, lp(dash dash)) (area fb_subsidy_total gast, color(gs10%50)), ///
xtitle("Gas Tax (cents / gallon)") ytitle("Optimal annual subsidy (dollars)")  ///
yline(0, lc(black)) ///
yscale(range(-500 1000)) ylabel(-500(500)1000) ///
xline(`gastax', lp(solid) lc(red%50)) 

graph export "${ResultsOut}/Figures/subsidy_and_gastax_20240508.png", replace

/*Graph of optimal subsidy as a function of electricty prices*/

clear
set obs 1000
gen margprice = 10+30*_n/1000
gen tau_e = (margprice - `PMC_elec') / `BEVmpkwh'
gen p_e = margprice / `BEVmpkwh'

gen fb_subsidy_externality = `basevmt'*((`phi_g'-`tau_g')-(`phi_e'-tau_e)) / 100

gen fb_subsidy_internality = -(1-`gammahat')*`basevmt'*((p_e-`theta')) / 100

gen fb_subsidy_total = fb_subsidy_externality+fb_subsidy_internality

label var fb_subsidy_externality "Unpriced Externality (A)"
label var fb_subsidy_internality "Unpriced Internality (B)"
label var fb_subsidy_total "Total Subsidy (= A + B)"

set scheme s1color

graph twoway (line fb_subsidy_externality fb_subsidy_internality margprice, lp(dash dash)) (area fb_subsidy_total margprice, color(gs10%50)), ///
yline(0, lc(black)) ///
yscale(range(-500 1000)) ylabel(-500(500)1000) ///
xtitle("Electricity Price (cents per kwh))") ytitle("Optimal annual subsidy (dollars)") ///
xline(`marg_elec_price', lp(solid) lc(red%50)) 


graph export "${ResultsOut}/Figures/subsidy_and_pe_20240508.png", replace

